//
//  LeftDrawerController.m
//  Pianke
//
//  Created by BetaTheta on 16/1/16.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "LeftDrawerController.h"
#import "DrawerBarCell.h"
#import "UIViewController+MMDrawerController.h"
#import "MMDrawerBarButtonItem.h"
#import "Homepage/HomeTodayController.h"
#import "RadioPageController.h"
#import "ReadPageController.h"
#import "CommunityViewController.h"
#import "ShardPageController.h"
#import "ShopViewController.h"
#import "Settings/SettingsController.h"
#import "AppDelegate.h"
#import "SearchViewController.h"


@interface LeftDrawerController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign) BOOL isT;
@property (nonatomic, retain) HomeTodayController *hTVC;

@end

@implementation LeftDrawerController
- (void)dealloc
{
    [_tableView release];
    [_hTVC release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 背景图
//    UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.bounds];
//    imageview.image = [UIImage imageNamed:@"backgroud3.png"];
//    [self.view addSubview:imageview];
    // 背景颜色
    self.view.backgroundColor = [UIColor colorWithRed:70 / 256.0 green:70 / 256.0 blue:70 / 256.0 alpha:1];
    
    // 头像 + 用户名
    [self creatUserInfo];
   
    // 四个用户编辑button
    [self creatBtn];
    
    // 创建SearchButton
    [self creatSearchBtn];
    // tableView
    [self creatTableView];
    
    // 创建模块VC
    self.isT = NO;
#warning 把第二页视图对象写成属性 不然放在点击方法中创建时 每次都是新建的对象 无法返回上一个
    self.hTVC = [[HomeTodayController alloc] init];
    
    

}
// 用户头像+名
- (void) creatUserInfo
{
    UIImageView *headImg = [[UIImageView alloc] initWithFrame:CGRectMake(10, 40, 80, 80)];
    headImg.backgroundColor = [UIColor purpleColor];
    headImg.layer.cornerRadius = headImg.frame.size.width / 2;
    headImg.clipsToBounds = YES;
    //    headImg.image = [UIImage imageWithContentsOfFile:<#(nonnull NSString *)#>];
    [self.view addSubview:headImg];
    [headImg release];
    
    UILabel *userName = [[UILabel alloc] initWithFrame:CGRectMake(100, 70, 150, 40)];
    userName.backgroundColor = [UIColor magentaColor];
    [self.view addSubview:userName];
    [userName release];

}
// 创建四个用户编辑按钮
- (void) creatBtn
{
    UIButton *downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    downloadBtn.frame = CGRectMake(25, 150, 25, 25);
    [downloadBtn setBackgroundImage:[UIImage imageNamed:@"icon_xiazai.png"] forState:UIControlStateNormal];
//    downloadBtn.backgroundColor = [UIColor greenColor];
    [self.view addSubview:downloadBtn];
    
    CGRect bF = downloadBtn.frame;
    UIButton *likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    bF.origin.x += 80;
    likeBtn.frame = bF;
//    likeBtn.backgroundColor = [UIColor greenColor];
    [likeBtn setBackgroundImage:[UIImage imageNamed:@"icon_xihuan.png"] forState:UIControlStateNormal];
    [self.view addSubview:likeBtn];
    
    
    UIButton *messageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    bF.origin.x += 80;
    messageBtn.frame = bF;
//    messageBtn.backgroundColor = [UIColor greenColor];
    [messageBtn setBackgroundImage:[UIImage imageNamed:@"icon_xiaoxi.png"] forState:UIControlStateNormal];
    [self.view addSubview:messageBtn];
    
    UIButton *writeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    bF.origin.x += 80;
    writeBtn.frame = bF;
//    writeBtn.backgroundColor = [UIColor greenColor];
    [writeBtn setBackgroundImage:[UIImage imageNamed:@"icon_bianji.png"] forState:UIControlStateNormal];
    [self.view addSubview:writeBtn];

}
// 创建SearchButton
- (void)creatSearchBtn
{
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(25, 190, self.view.frame.size.width - 115, 40);
//    searchBtn.backgroundColor = [UIColor yellowColor];
    [searchBtn setImage:[UIImage imageNamed:@"search@2x"] forState:UIControlStateNormal];
    searchBtn.layer.cornerRadius = 10;
    searchBtn.clipsToBounds = YES;
    [searchBtn addTarget:self action:@selector(searchAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview: searchBtn];
}
// 创建表视图
- (void) creatTableView
{
    self.tableView = [[[UITableView alloc] init] autorelease];
    CGRect f = self.view.frame;
    f.origin.y += 240;
    f.size.height -= 330;
    _tableView.frame = f;
    _tableView.dataSource = self;
    _tableView.delegate  = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[DrawerBarCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DrawerBarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSArray *arr = @[@"首页", @"电台", @"阅读", @"社区", @"碎片", @"良品", @"设置"];
    NSArray *ImgNameArr = @[@"icon_home.png", @"icon_diantai.png", @"icon_read.png", @"icon_group.png", @"icon_suipian.png", @"icon_liangpin.png", @"icon_shezhi.png"];
    cell.pageName.text = arr[indexPath.row];
    cell.barView.image = [UIImage imageNamed:ImgNameArr[indexPath.row]];

    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

// searchButton 触发方法: 模态到search页面
- (void)searchAction
{
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    // 模态动画
    searchVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    
    [self presentViewController:searchVC animated:YES completion:^{
    }];

}
// cell触发方法->push 模块Page
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *App = [[UIApplication sharedApplication] delegate];
    // ->优化switch
    if (indexPath.row == 0) {
        [App.mmDrawer closeDrawerAnimated:YES completion:^(BOOL finished) {
        }];
        NSLog(@"~~~~~");
        [App.navi popToRootViewControllerAnimated:YES];
       
    } else if (indexPath.row == 1){
        [App.mmDrawer closeDrawerAnimated:YES completion:^(BOOL finished) {
        }];
        RadioPageController *radioVC = [[RadioPageController alloc] init];
        [App.navi pushViewController:radioVC animated:YES];
        [radioVC release];
    } else if (indexPath.row == 2){
        [App.mmDrawer closeDrawerAnimated:YES completion:^(BOOL finished) {
        }];
        ReadPageController *readVC = [[ReadPageController alloc] init];
        [App.navi pushViewController:readVC animated:YES];
        [readVC release];
        
    } else if (indexPath.row == 3){
        [App.mmDrawer closeDrawerAnimated:YES completion:^(BOOL finished) {
        }];
        CommunityViewController *communityVC = [[CommunityViewController alloc] init];
        [App.navi pushViewController:communityVC animated:YES];
        [communityVC release];
        
    } else if (indexPath.row == 4){
        [App.mmDrawer closeDrawerAnimated:YES completion:^(BOOL finished) {
        }];
        ShardPageController *shardVC = [[ShardPageController alloc] init];
        [App.navi pushViewController:shardVC animated:YES];
        [shardVC release];
        
    } else if (indexPath.row == 5){
        [App.mmDrawer closeDrawerAnimated:YES completion:^(BOOL finished) {
        }];
        ShopViewController *shopVC = [[ShopViewController alloc] init];
        [App.navi pushViewController:shopVC animated:YES];
        [shopVC release];
        
    } else if (indexPath.row == 6){
        [App.mmDrawer closeDrawerAnimated:YES completion:^(BOOL finished) {
        }];
        SettingsController *setVC = [[SettingsController alloc] init];
        [App.navi pushViewController:setVC animated:YES];
        [setVC release];
    }
    
//    [self.mm_drawerController toggleDrawerSide:(MMDrawerSideLeft) animated:YES completion:^(BOOL finished) {
//        
//    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
